"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.genCode = void 0;
const csharp = require("csharp");
const CodeWriter = require("./CodeWriter");
exports.genCode = (handler) => {
    let settings = handler.project.GetSettings("Publish").codeGeneration;
    // 获取包名，调用此接口会将中文转换为拼音
    let codePkgName = handler.ToFilename(handler.pkg.name);
    let exportCodePath = handler.exportCodePath + '/' + codePkgName;
    let namespaceName = codePkgName;

    if (settings.packageName) {
        namespaceName = settings.packageName + '.' + namespaceName;
    }

    let memberNamePrefix = settings.memberNamePrefix;

    let classes = handler.CollectClasses(settings.ignoreNoname, settings.ignoreNoname, null);
    /**检测并删除已存在的代码 */
    handler.SetupCodeFolder(exportCodePath, "cs");

    let getMemberByName = settings.getMemberByName;
    let classCnt = classes.Count;
    let writer = new CodeWriter.default({ blockFromNewLine: false, usingTabs: true });

    for (let i = 0; i < classCnt; i++) {
        let classInfo = classes.get_Item(i);
        if (classInfo == null) continue;
        let xml = handler.GetItemDesc(classInfo.res).Elements();
        let count = xml.Count;
        for (let i = 0; i < count; i++) {
            if (xml.get_Item(i).name == "displayList") {
                xml = xml.get_Item(i).Elements();
                count = xml.Count;
                break;
            }
        }
        let members = classInfo.members;
        writer.reset();

        writer.writeln('using FairyGUI;');
        writer.writeln('using FairyGUI.Utils;');
        writer.writeln();
        writer.writeln(`namespace ${namespaceName}`);
        writer.startBlock();
        writer.writeln(`public partial class ${classInfo.className} : ${classInfo.superClassName}`);
        writer.startBlock();

        let memberCnt = members.Count;
        for (let j = 0; j < memberCnt; j++) {
            let memberInfo = members.get_Item(j);
            writer.writeln(`public ${memberInfo.type} ${memberInfo.varName};`);
        }
        writer.writeln(`public const string URL = "ui://${handler.pkg.id}${classInfo.resId}";`);
        writer.writeln();

        writer.writeln(`public static ${classInfo.className} CreateInstance()`);
        writer.startBlock();
        writer.writeln(`return (${classInfo.className})UIPackage.CreateObject("${handler.pkg.name}", "${classInfo.resName}");`);
        writer.endBlock();
        writer.writeln();

        if (handler.project.type == csharp.FairyEditor.ProjectType.MonoGame) {
            writer.writeln("protected override void OnConstruct()");
            writer.startBlock();
        } else {
            writer.writeln('public override void ConstructFromXML(XML xml)');
            writer.startBlock();
            writer.writeln('base.ConstructFromXML(xml);');
            writer.writeln();
        }
        for (let j = 0; j < memberCnt; j++) {
            let memberInfo = members.get_Item(j);
            let customData = null;
            for (let m = 0; m < count; m++) {
                if (xml.get_Item(m).GetAttribute("name") == memberInfo.name) {
                    customData = xml.get_Item(m).GetAttribute("customData");
                    break;
                }
            }
            switch (memberInfo.group) {
                case 0:
                    if (getMemberByName)
                        writer.writeln(`${memberInfo.varName} = (${memberInfo.type})GetChild("${memberInfo.name}");`);
                    else
                        writer.writeln(`${memberInfo.varName} = (${memberInfo.type})GetChildAt(${memberInfo.index});`);

                    if (!!customData) {
                        let j_data = JSON.parse(customData);
                        if (j_data) {
                            if (!!j_data.translate && !!j_data.key) {
                                switch (j_data.type) {
                                    case 1:
                                        writer.writeln(`${memberInfo.varName}.${memberNamePrefix}Text.text = Src.Game.Util.GameUtil.tr("${j_data.key}");`);
                                        break;
                                    default:
                                        writer.writeln(`${memberInfo.varName}.text = Src.Game.Util.GameUtil.tr("${j_data.key}");`);
                                        break;
                                }
                            }
                        }
                    }
                    break;
                case 1:
                    if (getMemberByName)
                        writer.writeln(`${memberInfo.varName} = GetController("${memberInfo.name}");`);
                    else
                        writer.writeln(`${memberInfo.varName} = GetControllerAt(${memberInfo.index});`);
                    break;
                default:
                    if (getMemberByName)
                        writer.writeln(`${memberInfo.varName} = GetTransition("${memberInfo.name}");`)
                    else
                        writer.writeln(`${memberInfo.varName} = GetTransitionAt(${memberInfo.index});`)
                    break;
            }
        }

        writer.endBlock();
        writer.endBlock();
        writer.endBlock();
        writer.save(exportCodePath + '/' + classInfo.className + '.cs');
    }
    writer.reset();

    let binderName = codePkgName + 'Binder';

    writer.writeln('using FairyGUI;');
    writer.writeln();
    writer.writeln(`namespace ${namespaceName}`);
    writer.startBlock();
    writer.writeln(`public class ${binderName}`);
    writer.startBlock();

    writer.writeln('public static void BindAll()');
    writer.startBlock();

    for (let i = 0; i < classCnt; i++) {
        let classInfo = classes.get_Item(i);
        writer.writeln(`UIObjectFactory.SetPackageItemExtension(${classInfo.className}.URL, typeof(${classInfo.className}));`);
    }
    writer.endBlock();
    writer.endBlock();
    writer.endBlock();
    writer.save(exportCodePath + '/' + binderName + '.cs');
};